#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <error.h>
#include <string.h>
#include <time.h>

struct  mouse_keyvalue
{
	int lpress;
	int rpress;
	int x;
	int y;
};

int main()
{
 	 int fd=0;
	int choice=0;
	struct mouse_keyvalue mkeyv={0};
 	fd = open("/sys/kobj_mouse/mouseattr", O_WRONLY);   //open the file node  in sysfs
  	if(fd < 0) 
  	{
    		perror("Couldn't open vms file\n");
    		exit(-1);
 	 }
  //the while circle to  always  send  virtual mouse values
 	 while(1)  
 	 {	

		mkeyv.lpress=0; //new left key value
		mkeyv.rpress=0; //new right key value 
        	time_t timet;
       	 	srand((unsigned)time(&timet));  //to feed seed of rand 
        	mkeyv.x = rand()%30;
        	mkeyv.y = rand()%30;
		choice=rand()%3;
		//give four types of key  move
		switch(choice)
		{
			case 0:
				mkeyv.lpress=1;  //1  means left button  is beat 0 mean  no motion
				break;
			case 1:
				mkeyv.rpress=1;
				break;
			case 2:	
				mkeyv.lpress=1;
				mkeyv.rpress=1;
				mkeyv.x=-mkeyv.x;
				mkeyv.y=-mkeyv.y;
				break;
			case 3:
				mkeyv.x=-mkeyv.y;
				mkeyv.y=-mkeyv.y;
				break;
			default:
				break;
				
		}
	
		printf("Value  store in:%d  %d  %d  %d\n",mkeyv.lpress,mkeyv.rpress,mkeyv.x,mkeyv.y);
		write(fd,&mkeyv,sizeof(struct mouse_keyvalue));
    		sleep(1);
	
 	 }
  
  close(fd);
}


